<?php
include("model/orders.php");
class ordersController{
	
	function indexAction(){
		$smarty = $_SESSION['smarty'];
		
		$list_orders_box = $this->getListOrdersBox();
		$smarty->assign("list_orders_box",$list_orders_box);
		
		$content = $smarty->fetch("orders/orders.html");		
		$smarty->assign("content", $content);
		
		$smarty->display("homepage.html");
	}
	
	function getListOrdersBox(){
		$smarty 		= $_SESSION['smarty'];
		$orders_obj 	= new orders();
		$ordersList		= $orders_obj->getOrdersList();		
		$smarty->assign("ordersList", $ordersList);
		$html 			= $smarty->fetch("orders/orders_list.html");
		return $html;
	}
	
	function insertAction(){
		$orders_obj = new orders();
		
		$data 		= array();
		$data['ord_receivedate'] 	= $_REQUEST['ord_receivedate'];
		$data['ord_empreceive_id'] 	= $_REQUEST['ord_empreceive_id'];
		
		$data['ord_issuedate'] 		= $_REQUEST['ord_issuedate'];
		$data['ord_empissue_id'] 	= $_REQUEST['ord_empissue_id'];
		
		$data['ord_cus_id'] 		= $_REQUEST['ord_cus_id'];
		$data['ord_pay_id']			= $_REQUEST['ord_pay_id'];
	
		$result = $orders_obj->insertAOrder($data);
		if ($result == 1)
			$msg = "insert successfully";
		else if ($result == -1)
			$msg = "duplicated!";
		else
			$msg = "insert failed.";
			
		$smarty = $_SESSION['smarty'];		
		$smarty->assign("msg", $msg);
		$content = $smarty->fetch("orders/orders_insertResult.html");		
		$smarty->assign("content", $content);		
		$smarty->display("homepage.html");
		
	}
	
	function inactiveAction(){
		$ord_id 			= $_REQUEST['args'];
		$orders_obj		=	new orders();
		$result 		= $orders_obj->inactivateAOrder($ord_id);
		if ($result == 1)
			$msg = "inactivate successfully";		
		else
			$msg = "inactivate failed.";
			
		$smarty = $_SESSION['smarty'];		
		
		
		$list_orders_box = $this->getListOrdersBox();
		$smarty->assign("list_orders_box",$list_orders_box);
		$smarty->assign("msg", $msg);
		
		$content = $smarty->fetch("orders/orders.html");		
		$smarty->assign("content", $content);		
		$smarty->display("homepage.html");
	}
	
	function activeAction(){
		$ord_id 		= $_REQUEST['args'];
		$orders_obj 	= new orders();
		$result 		= $orders_obj->activateAOrder($ord_id);
		if ($result == 1)
			$msg = "activate successfully";		
		else
			$msg = "activate failed.";
			
		$smarty = $_SESSION['smarty'];		
		
		
		$list_orders_box = $this->getListOrdersBox();
		$smarty->assign("list_orders_box",$list_orders_box);
		$smarty->assign("msg", $msg);
		
		$content = $smarty->fetch("orders/orders.html");		
		$smarty->assign("content", $content);		
		$smarty->display("homepage.html");
	}
	
	function insertFormAction(){
		$smarty = $_SESSION['smarty'];		
		
		$employeesList = array();
		include("model/employees.php");
		$employees_obj = new employees();
		$employeesList = $employees_obj->getEmployeesList();
		
		$customerList = array();
		include("model/customers.php");
		$customers_obj = new customers();
		$customerList = $customers_obj->getCustomersList();
		
		
		$paymentsList = array();
		include("model/payments.php");
		$payments_obj = new payments();
		$paymentsList = $payments_obj->getPaymentsList();
		
		$smarty->assign("employeesList", $employeesList);
		$smarty->assign("paymentsList", $paymentsList);
		$smarty->assign("customerList", $customerList);
		
		
		$insertform_orders_box = $smarty->fetch("orders/orders_insert_form.html");
		
		$smarty->assign("insertform_orders_box",$insertform_orders_box);
		
		$content = $smarty->fetch("orders/orders.html");		
		$smarty->assign("content", $content);
		
		$smarty->display("homepage.html");
	}
	
	function editFormAction(){
		$ord_id 			= $_REQUEST['args'];
		$smarty 			= $_SESSION['smarty'];		
		
		$employeesList = array();
		include("model/employees.php");
		$employees_obj = new employees();
		$employeesList = $employees_obj->getEmployeesList();
		
		
		$customerList = array();
		include("model/customers.php");
		$customers_obj = new customers();
		$customerList = $customers_obj->getCustomersList();
		
		$paymentsList = array();
		include("model/payments.php");
		$payments_obj = new payments();
		$paymentsList = $payments_obj->getPaymentsList();
		
		$smarty->assign("employeesList", $employeesList);
		$smarty->assign("paymentsList", $paymentsList);
		$smarty->assign("customerList", $customerList);
		
		$orders_obj 		= new orders();
		$current_data 		= array();
		$current_data 		= $orders_obj->getOrdersInfo($ord_id);
		
		$smarty->assign("current_data", $current_data);
		
		$editform_orders_box = $smarty->fetch("orders/orders_edit_form.html");
		$smarty->assign("editform_orders_box",$editform_orders_box);
		
		$content = $smarty->fetch("orders/orders.html");		
		$smarty->assign("content", $content);
		
		$smarty->display("homepage.html");	
	}
	
	function editAction(){
		$orders_obj 	= new orders();
		$data 			= array();
		$data['ord_id'] 			= $_REQUEST['ord_id'];
		$data['ord_receivedate'] 	= $_REQUEST['ord_receivedate'];
		$data['ord_issuedate'] 		= $_REQUEST['ord_issuedate'];
		
		$data['ord_empreceive_id']	= $_REQUEST['ord_empreceive_id'];
		$data['ord_empissue_id']	= $_REQUEST['ord_empissue_id'];
		
		$data['ord_cus_id']		 	= $_REQUEST['ord_cus_id'];
		$data['ord_pay_id']		 	= $_REQUEST['ord_pay_id'];	
		
		
		$result = $orders_obj->editAOrder($data);
		
		if ($result == 1)
			$msg = "edit successfully";
		else
			$msg = "update failed.";
			
		$smarty = $_SESSION['smarty'];		
		$smarty->assign("msg", $msg);
		$content = $smarty->fetch("orders/orders_updateResult.html");		
		$smarty->assign("content", $content);		
		$smarty->display("homepage.html");
	}
	
	
	
}
?>